using System;
using System.Web;
using System.Collections.Generic;

namespace SharpPieces.Web.Controls
{

    public class DynamicImageHandler : IHttpHandler
    {

        // Fields

        internal const string KEY_PATH = "path";
        internal const string KEY_ENABLED = "enabled";
        internal const string KEY_WIDTH = "width";
        internal const string KEY_HEIGHT = "height";
        internal const string KEY_CACHE = "cache";
        internal const string KEY_404 = "404";

        private HttpContext context = null;


        // Methods

        /// <summary>
        /// Utility method that returns a query string representation based on the provided parameter values.
        /// </summary>
        /// <param name="absoluteImagePath">The absolute image path.</param>
        /// <param name="enabled">The enabled stateof the image.
        /// Set false just when the server handles the disabled image.</param>
        /// <param name="width">The width in pixels. Set null when the original size is requested or the image should be scaled by height.</param>
        /// <param name="height">The height. Set null when the original size is requested or the image should be scaled by width.</param>
        /// <param name="cacheDuration">The client cache duration in minutes. Set 0 or less when no client cache is needed.</param>
        /// <returns>A query string representation based on the provided parameter values.</returns>
        public static string ToQueryString(string absoluteImagePath, bool enabled, int? width, int? height, int cacheDuration, bool accept404)
        {
            return HttpContext.Current.Server.UrlEncode(string.Format(
                "{0}={1}&{2}={3}&{4}={5}&{6}={7}&{8}={9}&{10}={11}",
                DynamicImageHandler.KEY_PATH, absoluteImagePath,
                DynamicImageHandler.KEY_ENABLED, enabled,
                DynamicImageHandler.KEY_WIDTH, width.GetValueOrDefault(-1),
                DynamicImageHandler.KEY_HEIGHT, height.GetValueOrDefault(-1),
                DynamicImageHandler.KEY_CACHE, cacheDuration,
                DynamicImageHandler.KEY_404, accept404));
        }


        // Properties

        /// <summary>
        /// Gets the current context.
        /// </summary>
        /// <value>The current context.</value>
        protected HttpContext Context
        {
            get
            {
                if (null == this.context)
                {
                    this.context = HttpContext.Current;
                }
                return this.context;
            }
        }


        #region IHttpHandler Members

        public bool IsReusable
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public void ProcessRequest(HttpContext context)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion

    }

}
